<template>
  <ele-page flex-table>
    <!-- 搜索表单 -->
    <PageSearch @search="reload" />
    <ele-card class="page-flex-full">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        cache-key="MchSystemSupplierTable"
      >
        <template #toolbar>
          <el-button type="primary" class="ele-btn-icon" :icon="Plus" @click="openEdit()">
            新增
          </el-button>
        </template>
        <!-- 头像 -->
        <template #thumb="{ row }">
          <el-avatar :class="row.thumb ? 'block mx-auto' : ''" shape="square" :size="30" :src="row.thumb" :icon="UserFilled" />
        </template>
        <template #roles="{ row }">
          <el-tag
            v-for="item in row.roles"
            :key="item.id"
            size="small"
            :disable-transitions="true"
            style="margin-right: 6px"
          >
            {{ item.name }}
          </el-tag>
        </template>
        <template #status="{ row }">
          <el-tag v-if="row.status === 1" type="success">正常</el-tag>
          <el-tag v-else type="danger">停用</el-tag>
        </template>
        <template #action="{ row }">
          <el-link type="primary" underline="never" @click="openBan(row)">
            绑定仓库
          </el-link>
          <el-divider direction="vertical" />
          <el-link type="primary" underline="never" @click="openEdit(row)">
            编辑
          </el-link>
          <el-divider direction="vertical" />

          <el-popconfirm
            :width="170"
            confirm-button-type="danger"
            :title="`确定删除用户『${row.nickname || row.realname}』吗?`"
            @confirm="remove(row.id)"
          >
            <template #reference>
              <el-link type="danger" underline="never">
                删除
              </el-link>
            </template>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 编辑弹窗 -->
    <PageEdit v-model="showEdit" :data="current" @done="reload" />

    <WarehouseBan v-model="showBan" :data="current" :type="2" @done="reload" />
  </ele-page>
</template>

<script setup>
import {
  pageSupplier,
  removeSupplier,
} from '@/api/mch-system/supplier'
import { Plus, UserFilled } from '@element-plus/icons-vue'
import WarehouseBan from '../user/components/ban-warehouse.vue'
import PageEdit from './components/edit.vue'
import PageSearch from './components/search.vue'

defineOptions({
  name: 'SystemMchSupplier',
})

/** 表格实例 */
const tableRef = ref(null)

/** 表格列配置 */
const columns = ref([
  {
    prop: 'id',
    label: 'ID',
    width: 50,
    showOverflowTooltip: true,
    fixed: 'left',
  },
  {
    prop: 'name',
    label: '货主名称',
    showOverflowTooltip: true,
    minWidth: 100,
  },
  {
    prop: 'nickname',
    label: '用户名',
    showOverflowTooltip: true,
    minWidth: 100,
  },
  {
    prop: 'username',
    label: '登录账号',
    showOverflowTooltip: true,
    minWidth: 100,
  },
  {
    prop: 'mobile',
    label: '手机号',
    minWidth: 100,
  },
  {
    columnKey: 'thumb',
    label: '头像',
    minWidth: 60,
    slot: 'thumb',
    align: 'center',
  },
  {
    prop: 'status',
    label: '状态',
    minWidth: 100,
    slot: 'status',
    align: 'center',
    formatter: row => row.status === 1 ? '正常' : '禁用',
  },
  {
    prop: 'last_login_ip',
    label: '最后登录IP',
    width: 160,
    align: 'center',
  },
  {
    prop: 'last_login_time',
    label: '最后登录时间',
    width: 160,
    formatter: (row, column, cellValue) => {
      return cellValue ? utils.formatDate(cellValue) : '-'
    },
    align: 'center',
  },
  {
    prop: 'create_time',
    label: '创建时间',
    width: 160,
    formatter: (row, column, cellValue) => {
      return cellValue ? utils.formatDate(cellValue) : '-'
    },
    align: 'center',
  },
  {
    prop: 'update_time',
    label: '更新时间',
    width: 160,
    formatter: (row, column, cellValue) => {
      return cellValue ? utils.formatDate(cellValue) : '-'
    },
    align: 'center',
  },
  {
    columnKey: 'action',
    label: '操作',
    width: 200,
    slot: 'action',
    align: 'right',
    fixed: 'right',
  },
])

/** 当前编辑数据 */
const current = ref(null)

/** 是否显示编辑弹窗 */
const showEdit = ref(false)
const showBan = ref(false)

/** 表格数据源 */
function datasource({ page, limit, where, orders }) {
  return pageSupplier({ ...where, ...orders, page, limit })
}

/** 搜索 */
function reload(where) {
  tableRef.value?.reload?.({ page: 1, where })
}

/** 打开编辑弹窗 */
function openEdit(row) {
  current.value = row ?? null
  showEdit.value = true
}

/** 打开绑定弹窗 */
function openBan(row) {
  current.value = row ?? null
  showBan.value = true
}

/** 删除用户 */
function remove(id) {
  const ids = [].concat(id)
  const loading = utils.message('请求中..', 'loading')
  removeSupplier(ids)
    .then((res) => {
      utils.message(res.msg, res.success, reload)
    })
    .finally(() => {
      loading.close()
    })
}
</script>
